﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Yugi.Client.Card;
using System.Windows;
using System.Threading;
namespace Yugi.Client
{
    public partial class SupervisorModel 
    {
        /// <summary>
        /// // Nhan ID 2 ng  choi
        /// </summary>
        /// <param name="param"></param>
        private void ProcessFromServer_GAME_BEGIN(params object[] param)
        {
            //Nguoi Choi
            Nguoi_Choi = new PlayerModel{ YPlayer= new Player(), YSupervisorModel= this};
            Nguoi_Choi.YPlayer.ID = (int)param[0];
            Mo_Nguoi_Choi = new TombModel { YSupervisorModel = this };

            //Doi Thu
            Doi_Thu = new PlayerModel { YPlayer = new Player(), YSupervisorModel = this };
            Doi_Thu.YPlayer.ID = (int)param[1];
            Mo_Doi_Thu = new TombModel { YSupervisorModel = this };

            //Ban Choi
            Turn = new TurnModel { YSupervisorModel = this };
            this.TableGame = new TableModel {YSupervisorModel=this,Table= new Table() };

            this.Viewdetail = new ViewDetailModel { YSupervisorModel = this };

            // Khoi Gan Skip
            //if (Nguoi_Choi.YPlayer.ID < Doi_Thu.YPlayer.ID)
            //{
            //    this.AllowSkip = true;
            //}
            //else
            //{
            //    this.AllowSkip = false;
            //}
            // Init Cua So Game
            InitCuaSo(this);
        }
        /// <summary>
        /// Nhan 5 la bai
        /// </summary>
        /// <param name="param"></param>
        private void ProcessFromServer_GAME_CHIABAI(params object[] param)
        {
            for (int i = 0; i < 5; i++)
            {
                Nguoi_Choi.YListCard.Add(new CardModel((YBaseCard) param[i],this));
                Doi_Thu.YListCard.Add(new CardModel(null, this));
            }

            Nguoi_Choi.OnChange("YListCard");
            Doi_Thu.OnChange("YListCard");
            this.SendMessageToServer(Common.YMSG.GAME_CHIABAIXONG);
        }
        
        /// <summary>
        /// Ket thuc Game
        /// </summary>
        /// <param name="param"></param>
        private void ProcessFromServer_GAME_END(params object[] param)
        {
            //this.SendMessageToServer(Common.YMSG.PLAYER_LOGOUT);
            //Action p =  () =>
            //{
            //    ((Window)this.win.Parent).Close();
            //};
            //this.win.Dispatcher.Invoke(p);
           
        }


        private void ProcessFromServer_CARD_XOABAI_DOITHU_TAY(params object[] param)
        {
            this.Doi_Thu.Presenter.Do(ClientMSG.XoaBaiDoiThuTay);
        }
        private void ProcessFromServer_CARD_RUTBAI_DOITHU(params object[] param)
        {
            this.Doi_Thu.Presenter.Do(ClientMSG.ThemBaiDoiThuTay);
        }

        private void ProcessFromServer_GAME_RESET(params object[] param)
        {

            this.Nguoi_Choi.Reset();
            this.Doi_Thu.Reset();
            this.Mo_Nguoi_Choi.Reset();
            this.Mo_Doi_Thu.Reset();
            this.TableGame.Reset();
            this.Mo_Doi_Thu.Reset();
            this.Mo_Nguoi_Choi.Reset();


            this.Nguoi_Choi.Presenter.Do(ClientMSG.Reset);
            this.Doi_Thu.Presenter.Do(ClientMSG.Reset);
            this.TableGame.Presenter.Do(ClientMSG.Reset);
            this.Mo_Nguoi_Choi.Presenter.Do(ClientMSG.Reset);
            this.Mo_Doi_Thu.Presenter.Do(ClientMSG.Reset);
            this.Nguoi_Choi.Health_Presenter.Do(ClientMSG.Reset);
            this.Doi_Thu.Health_Presenter.Do(ClientMSG.Reset);
            this.Nguoi_Choi.Deck_Presenter.Do(ClientMSG.Reset);
            this.Doi_Thu.Deck_Presenter.Do(ClientMSG.Reset);
            this.TableGame.Presenter.Do(ClientMSG.Reset);

            ((Action)this.win.DataContext)();
        }

        private void ProcessFromServer_CARD_INDECK(params object[] param)
        {
            int number = (int)param[0];
            YBaseCard temp;
            for (int i = 0; i < number; i++)
            {
                temp=(YBaseCard)param[i + 1];
                this.Nguoi_Choi.YDeck.Add(new CardModel(temp,this));
            }
        
        }

        private void ProcessFromServer_RPS_Hand(params object[] param)
        {
            RPSHand((int)param[0], (int)param[1]);
        }
        private void ProcessFromServer_RPS_Chose(params object[] param)
        {
            RPSChose((int)param[0], (int)param[1]);
        }

        private void ProcessFromServer_RPS_CanClick(params object[] param)
        {
            RPSCanClick();
        }

        private void ProcessFromServer_RPS_Draw(params object[] param)
        {
            RPSDraw();
        }

        public Action<int, int> RPSHand;
        public Action<int, int> RPSChose;
        public Action RPSCanClick;
        public Action RPSFinish;
        public Action RPSDraw;

    }
}
